# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
########################################
# 
# 功能：把文件夹打为zip
# 与cocos的打包zip方式有点区别，先放着
# 
########################################
import os
import zipfile
import tarfile

#打包目录为zip文件（未压缩）
def make_zip(source_dir, output_filename):
  zipf = zipfile.ZipFile(output_filename, 'w')
  pre_len = len(os.path.dirname(source_dir))
  for parent, dirnames, filenames in os.walk(source_dir):
    for filename in filenames:
      pathfile = os.path.join(parent, filename)
      arcname = pathfile[pre_len:].strip(os.path.sep)   #相对路径
      zipf.write(pathfile, arcname)
  zipf.close()

#整个文件夹打包压缩
def make_targz(source_dir,output_filename):
  with tarfile.open(output_filename, "w:gz") as tar:
  	arcname=os.path.basename(source_dir)
  	#print(arcname)
  	tar.add(source_dir,arcname=os.path.basename(source_dir))

#逐个添加文件打包，未打包空子目录。可过滤文件。
#如果只打包不压缩，将"w:gz"参数改为"w:"或"w"即可。
def make_targz_one_by_one(source_dir,output_filename):
  tar = tarfile.open(output_filename,"w:")
  for root,dirs,files in os.walk(source_dir):
    for name in files:
    	if name.endswith(".lua"):
	      pathfile = os.path.join(root, name)
	      pathfile2 = pathfile.replace(os.getcwd()+"/scripts/","")
	      print(pathfile)
	      #arcname=os.path.basename(source_dir)
	      tar.add(pathfile,pathfile2)
  tar.close()

if __name__ == "__main__":
    curPath = os.getcwd()
    upPath = os.path.dirname(os.getcwd())#取当前目录上一级目录
    #curPath = raw_input('Please enter the filename:').strip()
    #make_zip(curPath+"/scripts",curPath+"/test.zip")
    #make_targz(curPath+"/scripts",curPath+"/test.zip")
    #make_targz_one_by_one(curPath+"/scripts",curPath+"/game.zip")
    print(curPath)
    #cmdGame= "sh $QUICK_COCOS2DX_ROOT/bin/compile_scripts.sh -i "+ curPath+"/scripts -o game.zip"
    #os.system(cmdGame)